package com.cgah

import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.test.ControllerUnitTestCase
import grails.converters.XML
import util.converters.JSON

// 1. CANNOT EXTEND GrailsUnitTestCase BECAUSE THAT POS FAILS
// 2. MUST loadCodec(JSON) else json parsing fails sheesh.
abstract class AbstractUnitTest extends ControllerUnitTestCase {

    def session
    def params
    def redirectParams
    def flash


    void setupForClass(GroovyObject go) {
        go.class.metaClass.getParams = {-> params }
        go.class.metaClass.redirect = { Map args -> redirectParams = args  }
        go.class.metaClass.getSession = {-> session }
        go.class.metaClass.getFlash = {-> flash }

        def logger = new Expando(debug: { println it }, info: { println it },
                warn: { println it }, error: { println it })
        go.class.metaClass.getLog = {-> logger }
    }

    void setUp() {
        super.setUp()
        // WOW GRAILS = FAILS REALLY? THESE ARE NOT LOADED, REALLY? REALLY?
        // OMG, OMG OMG OMG OMG FAILS
        loadCodec(JSON)
        loadCodec(XML)

        session = [:]
        params = [:]
        redirectParams = [:]
        flash = [:]

    }

    protected void tearDown() {
        flash = null;
        redirectParams = null;
        params = null;
        session = null;
        super.tearDown()
    }

}
